<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>androrig.m</title>
<link rel="stylesheet" type="text/css" href="../../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>model</span>=<span class=defun_name>androrig</span>(<span class=defun_in>distrib,options,init_model</span>)<br>
<span class=h1>%&nbsp;ANDRORIG&nbsp;Original&nbsp;method&nbsp;to&nbsp;solve&nbsp;the&nbsp;Anderson-Bahadur's&nbsp;task.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;androrig(distrib)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;androrig(distrib,options)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;androrig(distrib,options,init_model)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;It&nbsp;solves&nbsp;the&nbsp;original&nbsp;Anderson&nbsp;task&nbsp;[Anderson62].&nbsp;The&nbsp;goal&nbsp;is&nbsp;to&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;find&nbsp;binary&nbsp;linear&nbsp;classifier&nbsp;which&nbsp;minimizes&nbsp;probability&nbsp;of&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;misclassification.&nbsp;The&nbsp;class&nbsp;conditional&nbsp;probability&nbsp;distributions&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;are&nbsp;Gaussians.&nbsp;The&nbsp;a&nbsp;prior&nbsp;probabilities&nbsp;is&nbsp;unknown.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;androrig(&nbsp;distrib&nbsp;)&nbsp;solves&nbsp;the&nbsp;original&nbsp;Anderson's&nbsp;task&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;given&nbsp;two&nbsp;Gaussians&nbsp;distributions.&nbsp;The&nbsp;structure&nbsp;distrib&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;<span class=help_field>contains:</span></span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Mean&nbsp;[dim&nbsp;x&nbsp;2]&nbsp;Matrix&nbsp;containing&nbsp;mean&nbsp;vectors&nbsp;of&nbsp;the&nbsp;first&nbsp;and</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;second&nbsp;class&nbsp;distributions.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Cov&nbsp;&nbsp;[dim&nbsp;x&nbsp;dim&nbsp;x&nbsp;2]$&nbsp;Matrix&nbsp;containing&nbsp;covariance&nbsp;matrices&nbsp;of&nbsp;the</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;first&nbsp;and&nbsp;second&nbsp;distribution.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;androrig(&nbsp;distrib,&nbsp;options&nbsp;)&nbsp;allows&nbsp;to&nbsp;specify&nbsp;the&nbsp;maximal&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;number&nbsp;of&nbsp;iterations&nbsp;options.tmax&nbsp;and&nbsp;the&nbsp;distance&nbsp;to&nbsp;the</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;optimal&nbsp;solution&nbsp;options.eps&nbsp;defining&nbsp;the&nbsp;stopping&nbsp;condition.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;androrig(&nbsp;distrib,&nbsp;options,&nbsp;init_model&nbsp;)&nbsp;allows&nbsp;to&nbsp;specify&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;initial&nbsp;point&nbsp;init_model.gamma.&nbsp;The&nbsp;initial&nbsp;value&nbsp;of&nbsp;the</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;counter&nbsp;of&nbsp;iterations&nbsp;can&nbsp;be&nbsp;specified&nbsp;in&nbsp;options.t.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;distrib&nbsp;[struct]&nbsp;Two&nbsp;Gaussians:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Mean&nbsp;[&nbsp;dim&nbsp;x&nbsp;2]&nbsp;Mean&nbsp;veactors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Cov&nbsp;&nbsp;[&nbsp;dim&nbsp;x&nbsp;dim&nbsp;x&nbsp;2]&nbsp;Covariance&nbsp;matrices.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;options&nbsp;[struct]&nbsp;Defines&nbsp;stopping&nbsp;condition:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.tmax&nbsp;[1x1]&nbsp;Maximal&nbsp;number&nbsp;of&nbsp;iteration.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.eps&nbsp;[1x1]&nbsp;Closeness&nbsp;to&nbsp;the&nbsp;optimal&nbsp;solution.&nbsp;If&nbsp;eps=0&nbsp;the</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;algorithm&nbsp;converges&nbsp;to&nbsp;the&nbsp;optimal&nbsp;solution&nbsp;but&nbsp;it&nbsp;does&nbsp;not</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;have&nbsp;to&nbsp;stop&nbsp;(default&nbsp;0.001).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;init_model&nbsp;[struct]&nbsp;Init&nbsp;model:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.gamma&nbsp;[1x1]&nbsp;Auxciliary&nbsp;variable&nbsp;(default&nbsp;1).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.t&nbsp;[1x1]&nbsp;(optional)&nbsp;Counter&nbsp;of&nbsp;iterations.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Binary&nbsp;linear&nbsp;classifier:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.W&nbsp;[dim&nbsp;x&nbsp;1]&nbsp;Normal&nbsp;vector&nbsp;the&nbsp;found&nbsp;hyperplane&nbsp;W'*x+b=0.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.b&nbsp;[1x1]&nbsp;Bias&nbsp;of&nbsp;the&nbsp;hyperplane.</span><br>
<span class=help>%&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.err&nbsp;[1x1]&nbsp;Probability&nbsp;of&nbsp;misclassification.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.t&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;iterations.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.r1&nbsp;[1x1]&nbsp;Mahalanobis&nbsp;distance&nbsp;of&nbsp;the&nbsp;first&nbsp;Gaussian&nbsp;to&nbsp;the</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;found&nbsp;hyperplane.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.r2&nbsp;[1x1]&nbsp;Mahalanobis&nbsp;distance&nbsp;of&nbsp;the&nbsp;second&nbsp;Gaussian&nbsp;to&nbsp;the</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;found&nbsp;hyperplane.&nbsp;In&nbsp;the&nbsp;optimal&nbsp;solution&nbsp;r1&nbsp;=&nbsp;r2.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;.exitflag&nbsp;[1x1]&nbsp;0&nbsp;...&nbsp;maximal&nbsp;number&nbsp;of&nbsp;iterations&nbsp;tmax&nbsp;exceeded.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;...&nbsp;condition&nbsp;delta&nbsp;&lt;&nbsp;eps&nbsp;satisfied.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.delta&nbsp;[1x1]&nbsp;Indicates&nbsp;distance&nbsp;from&nbsp;the&nbsp;optimal&nbsp;solution.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.gamma&nbsp;[1x1]&nbsp;Auxciliary&nbsp;variable.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;=&nbsp;load('riply_trn');</span><br>
<span class=help>%&nbsp;&nbsp;distrib&nbsp;=&nbsp;mlcgmm(data);</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;androrig(distrib);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;pandr(&nbsp;model,&nbsp;distrib&nbsp;);</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;GANDERS,&nbsp;EANDERS,&nbsp;GGRADANDR,&nbsp;LINCLASS.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2003,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;20-may-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;24-Feb-2003,&nbsp;VF</span><br>
<br>
<hr>
<span class=comment>%&nbsp;process&nbsp;input&nbsp;arguments</span><br>
<span class=comment>%&nbsp;-----------------------------</span><br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;1,<br>
&nbsp;&nbsp;<span class=error>error</span>(<span class=quotes>'Not&nbsp;enough&nbsp;input&nbsp;arguments.'</span>);<br>
<span class=keyword>end</span><br>
<br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;2,&nbsp;options&nbsp;=&nbsp;[];&nbsp;<span class=keyword>else</span>&nbsp;options&nbsp;=&nbsp;c2s(&nbsp;options&nbsp;);&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'eps'</span>),&nbsp;options.eps&nbsp;=&nbsp;0.001;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'tmax'</span>),&nbsp;options.tmax&nbsp;=&nbsp;inf;&nbsp;<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;get&nbsp;paramaters&nbsp;of&nbsp;distributions</span><br>
<span class=keyword>if</span>&nbsp;isfield(distrib,&nbsp;<span class=quotes>'y'</span>&nbsp;)&nbsp;&&nbsp;(distrib.y(1)&nbsp;~=&nbsp;distrib.y(2)),<br>
&nbsp;&nbsp;inx1&nbsp;=&nbsp;2;&nbsp;inx2&nbsp;=&nbsp;1;<br>
<span class=keyword>else</span><br>
&nbsp;&nbsp;inx1&nbsp;=&nbsp;1;&nbsp;inx2&nbsp;=&nbsp;2;<br>
<span class=keyword>end</span><br>
m1=distrib.Mean(:,inx1);<br>
m2=distrib.Mean(:,inx2);<br>
dim=length(m1);<br>
C1=distrib.Cov(:,:,inx1);<br>
C2=distrib.Cov(:,:,inx2);<br>
<br>
<span class=comment>%&nbsp;Inicialization&nbsp;</span><br>
<span class=comment>%&nbsp;-----------------------------------------</span><br>
gamma=1;<br>
delta=inf;<br>
t=0;<br>
<br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;==&nbsp;3,&nbsp;,<br>
&nbsp;&nbsp;gamma&nbsp;=&nbsp;init_model.gamma;<br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;isfield(&nbsp;init_model,&nbsp;<span class=quotes>'t'</span>),&nbsp;t&nbsp;=&nbsp;init_model.t;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;Main&nbsp;loop&nbsp;cycle</span><br>
<span class=comment>%---------------------------------</span><br>
<span class=keyword>while</span>&nbsp;options.tmax&nbsp;&gt;&nbsp;t&nbsp;&&nbsp;delta&nbsp;&gt;&nbsp;options.eps,<br>
&nbsp;&nbsp;&nbsp;t=t+1;<br>
<br>
&nbsp;&nbsp;&nbsp;lambda=1/(1+gamma);<br>
<br>
&nbsp;&nbsp;&nbsp;W=inv((1-lambda)*C1+lambda*C2)*(m1-m2);<br>
&nbsp;&nbsp;&nbsp;gamma=sqrt((W<span class=quotes>'*C2*W)/(W'</span>*C1*W));<br>
<br>
&nbsp;&nbsp;&nbsp;delta=abs(&nbsp;gamma&nbsp;-&nbsp;(1-lambda)/lambda&nbsp;);<br>
<span class=keyword>end</span><br>
<br>
b1&nbsp;=&nbsp;-lambda*(W<span class=quotes>'*C2*W)-(W'</span>*m2);<br>
b2&nbsp;=&nbsp;(1-lambda)*(W<span class=quotes>'*C1*W)-(W'</span>*m1);<br>
b&nbsp;=&nbsp;(b1+b2)/2;<br>
<br>
r1&nbsp;=&nbsp;(W<span class=quotes>'*m1&nbsp;+&nbsp;b)/sqrt(W'</span>*C1*W);<br>
r2&nbsp;=&nbsp;-(W<span class=quotes>'*m2&nbsp;+&nbsp;b)/sqrt(W'</span>*C2*W);<br>
minr=min([r1&nbsp;r2]);<br>
<br>
<span class=comment>%&nbsp;--&nbsp;output&nbsp;model&nbsp;-----------------</span><br>
model.W&nbsp;=&nbsp;W;<br>
model.b=b;<br>
model.err&nbsp;=&nbsp;1-cdf(<span class=quotes>'norm'</span>,minr,0,1);<br>
model.t&nbsp;=&nbsp;t;<br>
model.delta&nbsp;=&nbsp;delta;<br>
model.gamma&nbsp;=&nbsp;gamma;<br>
model.r1=r1;<br>
model.r2=r2;<br>
model.r=minr;<br>
<br>
<span class=keyword>if</span>&nbsp;delta&nbsp;&lt;=&nbsp;options.eps,&nbsp;<br>
&nbsp;&nbsp;model.exitflag&nbsp;=&nbsp;1;<br>
<span class=keyword>else</span><br>
&nbsp;&nbsp;model.exitflag&nbsp;=&nbsp;0;<br>
<span class=keyword>end</span><br>
<br>
model.fun=<span class=quotes>'linclass'</span>;<br>
<br>
<span class=jump>return</span>;<br>
</code>
